package cn.edu.nju.software.cdiy.dao.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

import cn.edu.nju.software.cdiy.bean.Uorder;
import cn.edu.nju.software.cdiy.bean.User;
import cn.edu.nju.software.cdiy.bean.Value;
import cn.edu.nju.software.cdiy.dao.UorderDao;
import cn.edu.nju.software.cdiy.dao.ValueDao;
import cn.edu.nju.software.cdiy.util.BusinessConstant;

public class UorderDaoImpl implements UorderDao {

	@Autowired
	private SessionFactory sessionFactory;
	@Autowired
	private ValueDao valueDao;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public void setValueDao(ValueDao valueDao) {
		this.valueDao = valueDao;
	}

	@Override
	public Uorder saveUorder(Uorder uorder) {
		Session session = sessionFactory.getCurrentSession();

		try {
			session.save(uorder);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return uorder;
	}

	@Override
	public List<Uorder> getUserUorders(User user) {
		Session session = sessionFactory.getCurrentSession();
		
		List list = null;
		try {
			String sql="from Uorder o where o.user=:userid  order by o.time desc";
			Query query = session
					.createQuery(sql);
			query.setParameter("userid", user);
			/*
			query.setParameter(
					"state",
					valueDao.getValue(BusinessConstant.UORDER_STATE6));
					*/
			list = query.list();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public Uorder getUorderById(Long uorderId) {
		Session session = sessionFactory.getCurrentSession();
		Uorder returnUorder = null;
		try {
			returnUorder = (Uorder) session.load(Uorder.class, uorderId);
		} catch (Exception e) {

			e.printStackTrace();
		}
		return returnUorder;
	}

	// public static final String UORDER_TYPE1="订单待处理";
	// public static final String UORDER_TYPE2="订单已处理";
	// public static final String UORDER_TYPE3="订单已发货";
	// public static final String UORDER_TYPE4="订单完成";
	// public static final String UORDER_TYPE5="订单失败";
	// public static final String UORDER_TYPE6="订单取消";
	// 用于用户界面，分类Uorder
	@Override
	public List<Uorder> getUorderState(User user, Value state) {
		Session session = sessionFactory.getCurrentSession();
		
		List<Uorder> list = null;
		try {
			Query query = session
					.createQuery("from Uorder o where o.user=:userid and o.state=:state order by o.time desc");
			query.setParameter("userid", user);
			query.setParameter("state", state);
			list = query.list();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	// public static final String UORDER_TYPE1="订单待处理";
	// public static final String UORDER_TYPE2="订单已处理";
	// public static final String UORDER_TYPE3="订单已发货";
	// public static final String UORDER_TYPE4="订单完成";

	// public static final String UORDER_TYPE5="订单失败";
	// public static final String UORDER_TYPE6="订单取消";
	// 用于管理员界面，分类Uorder
	@Override
	public List<Uorder> getAllUorder(Value state) {
		Session session = sessionFactory.getCurrentSession();
		
		List<Uorder> order = null;
		try {
			Query query = session
					.createQuery("from Uorder u where u.state=:state");
			query.setParameter("state", state);
			order = query.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return order;
	}

	@Override
	public List<Uorder> getAllUorderAndDate(Date startDate, Value state) {
		return null;
	}

	@Override
	public boolean saveUorderState(Uorder u, Value state) {
		Session session = sessionFactory.getCurrentSession();
	
		boolean returnValue = false;
		try {
			u = (Uorder) session.load(Uorder.class, u.getId());
			u.setState(state);
			returnValue = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return returnValue;
	}

}
